// Misc stuff
var debug = null;
var debug2 = null;
var debug2 = null;
var dummySprite = null;
var count = 0;

// Graphics
var canvas = null;
var ctx = null;

// Keys
var leftKey = Kb_trapkey("LEFT");
var rightKey = Kb_trapkey("RIGHT");
var thrustKey = Kb_trapkey("z");

// Global state management
var alreadyHooked = false;

const WIDTH = 192;
const HEIGHT = 96;

const GRAVITY = -0.03;

// The Ship
var theShip = new Ship();

// Game states
// 0 = demo
// 10 = setup
// 11 = ship exploded
// 12 = play
// 13 = end
var gState = 0;

// Time managment
var elapsed;
var preElapsed;

var MPF = 33;		// milliseconds per frame. 17 ~= 60fps. 33 ~= 30fps

// Notes: rgba(r,g,b,a)

// **********************************************************
// ** Framework Functions                                  **
// **********************************************************
function calibrationComplete()
{
	if (!alreadyHooked)
	{
		alreadyHooked = true;
		Gl_hook("draw()");	// This should only be called once.
		spout_init();
	}
	
	Sp_linuxcompatible = true;
	dummySprite = new Sp_Sprite();
	dummySprite.setImage("http://spout.googlecode.com/svn/trunk/penquin.png", 16, 16, 1, 1);
	dummySprite.switchOff();
	
	Gl_start();
}

function spout_init()
{
	debug = document.getElementById("debug");
	debug2 = document.getElementById("debug2");
	debug3 = document.getElementById("debug3");
	debug4 = document.getElementById("debug4");
	
   canvas = document.getElementById("canvas");
  	if (canvas.getContext)
   {
		ctx = canvas.getContext("2d");
	}
	else
	{
		alert("Could not get context");
	}

	theShip.thrust = new ParticleSystem();
	theShip.thrust.initialize(25);
	theShip.thrust.registerCallback(theShip);
	theShip.setPos(100.0, 75.0);
	theShip.boundsUpperX = 0;
	theShip.boundsUpperY = 0;
	theShip.boundsLowerX = WIDTH;
	theShip.boundsLowerY = HEIGHT;
	theShip.setGravity(GRAVITY);
	theShip.obstacles = new Obstacles();
	theShip.initialize();
	
	ctx.strokeRect(0, 0, WIDTH, HEIGHT);
}

function spout_resume()
{
	elapsed = new Date();
	preElapsed = new Date();
	Gl_start();
}

function spout_start()
{
	elapsed = new Date();
	preElapsed = new Date();
	Gl_setTimerSpeed(MPF);
	calibrationComplete();
}

function spout_stop()
{
	elapsed = new Date();
	preElapsed = new Date();
	Gl_stop();
}

function spout_reset()
{
	theShip.reset();
}

// **********************************************************
// ** Game Functions                                       **
// **********************************************************
// Use this function if you want the ship's thrust
// to effect the direction only when thrust is being applied.
function spout_trigger()
{
	theShip.triggerExplosion();
}

function spout_gravity()
{
	theShip.toggleGravity();
}

function draw() {
	elapsed = new Date();
	
	// Clear the canvas. Note clear only inside.
	// Leave the border untouched. This saves a stroke.
	ctx.fillStyle = "rgb(255,255,255)";
	ctx.fillRect (1, 1, WIDTH, HEIGHT);
	
	// Calc new ship position first.
	theShip.update(MPF);
	
	theShip.draw();

	// Clean the edges
	ctx.strokeStyle = "rgb(0,0,0)";
	ctx.strokeRect(0, 0, WIDTH, HEIGHT);
	
	preElapsed = new Date();
	var dt = preElapsed.getMilliseconds() - elapsed.getMilliseconds();
	debug.innerHTML = dt;
	
}

// **********************************************************
// ** Misc code. Can be removed                            **
// **********************************************************
